package com.foreknow.dao.impl;

import com.foreknow.bean.User;
import com.foreknow.dao.UserDao;
import com.foreknow.db.DBManager;
import com.foreknow.db.JdbcTemplate;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl extends BaseDao implements UserDao {
    @Override
    public int add(User user) throws SQLException {
        String sql = "insert into users(adminName,password) values (?,?)";
//        DBManager dbManager = DBManager.getInstance();
//        Connection conn = dbManager.getConnection();
//        //调用basedao中的方法
//        this.setConnection(conn);
        int isRight = jdbcTemplate.update(sql, user.getAdminName(), user.getPassword());
        return isRight;
    }

    @Override
    public User login(String adminName, String password) throws SQLException {
        String sql = "select * from users where adminname = ?and password = ?";
//        DBManager dbManager = DBManager.getInstance();
//        Connection conn = dbManager.getConnection();
//        this.setConnection(conn);
        EntityMapping entityMapping = mappingFactory.getmapping(MappingFactory.USER_MAPPING);
        List<Object> list = jdbcTemplate.query(sql, entityMapping, adminName, password);
        if (list.size() > 0) {
            User user = (User) list.get(0);
            return user;
        }
        return null;
    }
}


//    public static void main(String[] args) {
//        UserDaoImpl udil= new UserDaoImpl();
//        User user = new User();
//        user.setAdminName("tony");
//        user.setPassword("124521");
//        int a = udil.add(user);
//        System.out.println(a);

//        String name = "tony";
//        String password = "124521";
//        System.out.println(udil.login(name,password).getAdminName());
//    }
//}
